package com.fyl.leetcode.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:fyl
 * @date 2021/7/31 9:48
 * @Modified By:
 * @Modified Date:
 * @Description:
 */
public class PalindromePartitioning {
    public List<List<String>> partition(String s) {
        List<List<String>> result = new ArrayList<>();
        List<String> list = new ArrayList<>();
        dfs(s, result, list);
        return result;
    }

    private void dfs(String s, List<List<String>> result, List<String> list) {
        if (s.length() == 0) {
            result.add(new ArrayList<>(list));
            return;
        }

        for (int i = 0; i < s.length(); i++) {
            if (isPalindrome(s, 0, i)) {
                list.add(s.substring(0, i + 1));
                dfs(s.substring(i + 1), result, list);
                list.remove(list.size() - 1);
            }
        }
    }

    private boolean isPalindrome(String str, int start, int end) {
        while (start < end) {
            if (str.charAt(start++) != str.charAt(end--)) {
                return false;
            }
        }
        return true;
    }
}
